package ua.kpi_java_training5.kereia.module1;
import java.util.Scanner;
import java.util.ArrayList;
/**
 * Program find all polindroms and print that
 * @author Alex Kereia
 * @since 2013-05-18
 */
public class Polindroms {
    private ArrayList<String> polindroms;
    String [] input;
    public Polindroms(String [] a) {
        polindroms = new ArrayList<>();
        input = a;
    }
    
    public static void main(String[] args) {
        String textLine;
        textLine = inputTextLine();
        String[] arrayText = textLine.split("");
        Polindroms pol = new Polindroms(arrayText);
        pol.findPolindroms(pol.input);
        System.out.println(pol.getResult(pol.polindroms));
    }
    
    /** 
     * Metod which return input text;
     * @return String
     */
    public static String inputTextLine() {
        String text;
        Scanner scan = new Scanner(System.in);
        System.out.print("Write text:");
        text = scan.nextLine();
        return text;
    }
    
    /**
     * Method which find min polindroms in text line
     */
    public void findPolindroms(String [] input) {
        for(int i = 1;i<(input.length-2);i++) {
            
            boolean checkPolindrom = input[i].equals(input[i+2]);
            
            if(checkPolindrom) {
                checkLengthOfPolindrom(i + 1, input);
            }
        }
        
    }
    
    /**
     * Method which check length of polindrom
     * @param centerOfPolindrom 
     * @param input
     */
    public void checkLengthOfPolindrom(int centerOfPolindrom, String [] input) {
        for (int i = 1; i <= 10; i++) {
            if(i < ((input.length)/2)) {
                try {
                    boolean asd = input[(centerOfPolindrom-i)].equals(input[(centerOfPolindrom+i)]);
                    if(asd) {

                        continue;
                    }else{

                        savePolindromInList((centerOfPolindrom-i+1),(centerOfPolindrom+i-1), input);
                        break;
                    }
                } catch (ArrayIndexOutOfBoundsException ex) {

                    savePolindromInList((centerOfPolindrom-i+1),(centerOfPolindrom+i-1), input);
                    break;
                }
            } else {
                savePolindromInList((centerOfPolindrom-i+1),(centerOfPolindrom+i-1), input);
                break;
            }
        }
        
    }
    
    /**
     * Metod which save polindroms in ArrayList(), 
     * @param startNumberName
     * @param endNumberName 
     */
    public void savePolindromInList(int startNumberName, int endNumberName, String [] input) {
        String asd = "";
        for(int i = startNumberName;i<=(startNumberName+(endNumberName-startNumberName));i++) {
            
            asd = asd + input[i];
        }
        polindroms.add(asd);
    }
    
    
    /**
     * Method which print special simbols
     * @param specialSimbol 
     * @param value - how many simbols will be paint
     * @return String
     */
    public static String printSpecialSimbol(String specialSimbol, int value) {
        String result = "";
        for(int i = 0;i<value;i++) {
            result = result + specialSimbol;
        }
        return result;
    }
    
    /**
     * Method which return all polindrom which was find, as String line
     * @param polindroms - ArrayList() which contain found polindroms
     * @return String
     */
    public String getResult(ArrayList<String> polindroms) {
        String result = "";
        for(int i = 0;i<polindroms.size();i++) {
            result = result + polindroms.get(i);
            if(i <(polindroms.size()-1)) {
                result = result + printSpecialSimbol("|", 1);
            }
        }
        return result;
    }
    

    
}
